import { defineMock } from "umi";

// 生成标准格式的 token（包含角色信息）
const generateToken = (role: string) => `role_${role}_token`;
// 模拟用户数据库
const users: any = {
  admin: {
    id: 1,
    username: "admin",
    password: "123456",
    realName: "管理员",
    avatar: "https://picsum.photos/id/1005/200/200",
    permissions: ["admin", "user:read", "user:write","canNewsView_111"],
    token: generateToken('admin'),
  },
  user: {
    id: 2,
    username: "user",
    password: "123456",
    realName: "普通用户",
    avatar: "https://picsum.photos/id/1012/200/200",
    permissions: ["user:read","canNewsView_222"],
    token: generateToken('user'),
  },
};

export default defineMock({
  // 模拟登录接口
  "POST /api/login": (req, res) => {
    const { username, password } = req.body;

    // 简单验证（实际项目中应使用加密和更安全的验证方式）
    if (!username || !password) {
      return res.send({
        code: 400,
        msg: "用户名和密码不能为空",
        data: null,
      });
    }
    // 查找用户
    const user = users[username];

    // 用户不存在
    if (!user) {
      return res.send({
        code: 401,
        msg: "用户不存在",
        data: null,
      });
    }
    // 验证密码（这里简单假设密码为用户设置的密码）
    if (password !== user.password) {
      return res.send({
        code: 401,
        msg: "密码错误",
        data: null,
      });
    }
    // 登录成功，返回用户信息和token
    return res.send({
      code: 200,
      msg: "登录成功",
      data: {
        userInfo: {
          id: user.id,
          username: user.username,
          realName: user.realName,
          avatar: user.avatar,
          permissions: user.permissions,
        },
        token: user.token,
      },
    });
  },
  // 获取用户信息
  "GET /api/user/info": (req, res) => {
    const { token } = req.headers;
    // 验证token
    const user: any = Object.values(users).find((u: any) => u.token === token);
    if (!user) {
      return res.send({
        code: 401,
        msg: "未授权或token无效",
        data: null,
      });
    }
    // 返回用户信息
    return res.send({
      code: 200,
      msg: "获取用户信息成功",
      data: {
        id: user.id,
        username: user.username,
        realName: user.realName,
        avatar: user.avatar,
        permissions: user.permissions,
      },
    });
  },
  // 返回菜单信息
  "GET /api/menuList": (req,res) => {
     const menuList = [
      {
        path: "/dashboard",
        name: "统计分析",
        icon: "DashboardOutlined",
        children:[
          {
            name: "分析1",
            path: "/dashboard/analysis/111",
            icon: "AreaChartOutlined",
            is_source:true,
          },
          {
            name: "分析2",
            path: "/dashboard/analysis/2222",
            icon: "AreaChartOutlined",
            is_source:true,
          },
          {
            name: "监控",
            path: "/dashboard/monitor",
            icon: "MonitorOutlined",
          }
        ]
      },
      {
        path:'/docs',
        name: "文档管理",
      },
      {
        path: "/products",
        name: "产品管理",
        icon: "ProductOutlined",
      },
      {
        path:"/news/111",
        name:"新闻详情1",
        is_source: true, // 是否为资源（动态路由） 是：不动态注入， 否：动态注入
      },
      {
        path:"/news/222",
        name:"新闻详情2",
        is_source: true, // 是否为资源（动态路由） 是：不动态注入， 否：动态注入
      },
      {
        path:"/news/333/sss",
        name:"新闻详情3",
        is_source: true, // 是否为资源（动态路由） 是：不动态注入， 否：动态注入
      },
      {
        path:"/news/aaaa/settings",
        name:"新闻详情4",
        is_source: true, // 是否为资源（动态路由） 是：不动态注入， 否：动态注入
      }
    ];
    const { token } = req.headers;
    // 验证token
    const user: any = Object.values(users).find((u: any) => u.token === token);
    if (!user) {
      return res.send({
        code: 401,
        msg: "未授权或token无效",
        data: null,
      });
    }
    return res.send({
      code: 200,
      msg: "获取路由成功",
      data: menuList,
    });
  }
});
